<template>
  <div class="auto">
    <partial-view v-ref:pv>
      <owe-list :row='list' @refresh-center="refreshCenter"></owe-list>
    </partial-view>
    <div v-if="chargeView">
      <p v-if="row.f_totalsplit_type === '总表'"><strong>分表承担总量 (单位: 方): {{splitAllgas}}</strong></p>
      <machine-meter-center :data='model' :row='row'></machine-meter-center>
    </div>
  </div>
</template>
<script>
  import co from 'co'
  import OweList from './OweList'

  let load = function* (self, id) {
    let res = yield self.$refs.pv.load('rs/sql/getOverdue', {data: {f_user_id: id}})
    if (res.data) {
      for (let i = 0; i < res.data.length; i++) {
        res.data[i].f_user_id = res.data[i].f_user_id + ''
        let getSplit = yield self.$resetpost('rs/logic/splitInfo', {data: {totalInfo: res.data[i]}}, {
          resolveMsg: null,
          rejectMsg: '获取分表抄表信息出错!!!'
        })
        if (getSplit.data.length > 0) {
          for (let j = 0; j < getSplit.data.length; j++) {
            console.log('分表用量计算，。。。。。', self.splitAllgas, getSplit.data[j].f_oughtamount, (getSplit.data[j].f_share_gas - 0), (getSplit.data[j].f_oughtamount - 0) + (getSplit.data[j].f_share_gas - 0))
            self.splitAllgas = self.splitAllgas + (getSplit.data[j].f_oughtamount - 0)
          }
        }
      }
      self.list.metertitle = id
      self.list.rows = self.model.rows = [...self.model.rows, ...res.data]
      // 控制欠费列表承担量的显示
      self.model.f_totalsplit_type = self.row.f_totalsplit_type
    }
    // self.initData(self.row)
    // self.getRows(res.data)
    self.chargeView = true
  }

  /**
   *售气收费
   *机表收费组合组件，包括抄表列表以及机表收费主组件
   */
  export default {
    title: '机表收费',
    components: {OweList},
    data() {
      return {
        model: {
          rows: []
        },
        maxindex: 0,
        list: {
          metertitle: '',
          rows: []
        },
        splitAllgas: 0,
        chargeView: false
      }
    },
    props: ['row'],
    ready() {
      // this.model = Object.assign({}, this.model, this.row)
      this.getList(this.row.f_user_id)
    },
    methods: {
      refreshCenter (val) {
        console.log('刷新界面了，，', val)
        this.initData(this.row)
        console.log('初始化完毕...', this.model)
        this.getRows(val)
      },
      getList (id) {
        let getData = load(this, id)
        return co(getData)
      },
      initData (row) {
        let temp = {
          f_payment: ['现金缴费'],
          f_price_id: row.f_price_id,
          f_user_id: row.f_user_id,
          f_balance: row.f_balance,
          f_pregas: 0,
          f_preamount: 0,
          f_totalcost: 0,
          f_curbalance: 0,
          f_collection: '',
          f_overdue: 0,
          f_lowest: 0,
          f_print: ['专用收据']
        }
        console.log('before-=---model', this.model, temp)
        this.model = Object.assign({}, this.model, temp)
        console.log('after-=---model', this.model)
        // this.model.f_payment= ['现金缴费'],
        // this.model.f_price_id= row.f_price_id,
        // this.model.f_user_id= row.f_user_id,
        // this.model.f_balance= row.f_balance,
        // this.model.f_pregas= 0,
        // this.model.f_preamount= 0,
        // this.model.f_totalcost= 0,
        // this.model.f_curbalance= 0,
        // this.model.f_collection= '',
        // this.model.f_overdue= 0,
        // this.model.f_lowest= 0,
        // this.model.f_print= ['专用收据']
        // console.log('init---model', this.model)
      },
      getRows (val) {
        if (val.length === 0) {
          this.model.f_lowest = this.model.f_totalcost = (this.model.f_balance - 0) > 0 ? 0 : 0 - (this.model.f_balance - 0)
          this.model.f_collection = Math.trunc(this.model.f_totalcost) > 0 ? Math.trunc(this.model.f_totalcost) : this.model.f_totalcost
          this.model.start_index = -1
          return
        }
        // logic.f_lowest = logic.f_lowest - (logic.f_balance - 0) > 0 ? logic.f_lowest - (logic.f_balance - 0) : 0
        console.log('getRows..model', this.model)
        this.model.f_lowest = this.model.f_lowest - (this.model.f_balance - 0)

        let firstOverdue = val.findIndex((item) => {return item.overdue > 0})
        console.log('查询第一笔滞纳金位置', firstOverdue)
        this.model.start_index = firstOverdue
        if (firstOverdue === -1) {
          console.log('qianfei ....', val)
          this.model.f_lowest = this.model.f_lowest + val[0].f_oughtfee
          this.model.f_overdue = 0
          for (let i = 0; i < val.length; i++) {
            this.model.f_pregas = this.model.f_pregas + val[i].f_oughtamount
            this.model.f_preamount = this.model.f_preamount + val[i].f_oughtfee
          }
        } else {
          for (let i = 0; i < val.length; i++) {
            this.model.f_pregas = this.model.f_pregas + val[i].f_oughtamount
            this.model.f_preamount = this.model.f_preamount + val[i].f_oughtfee
            this.model.f_overdue = this.model.f_overdue + val[i].overdue
          }
          this.model.end_index = this.getMaxIndex(val)
          for (let i = 0; i <= this.getMaxIndex(val); i++) {
            this.model.f_lowest = this.model.f_lowest + val[i].f_oughtfee + val[i].overdue
          }
        }
        // logic.f_pregas = logic.f_pregas - this.splitAllgas
        this.model.f_overdue = this.model.f_overdue.toFixed(2)
        console.log('计算应交金额', (this.model.f_preamount - 0) + (this.model.f_overdue - 0))
        this.model.f_totalcost = (this.model.f_preamount - 0) + (this.model.f_overdue - 0) - (this.model.f_balance - 0) > 0 ? (this.model.f_preamount - 0) + (this.model.f_overdue - 0) - (this.model.f_balance - 0) : 0
        this.model.f_collection = Math.trunc(this.model.f_totalcost)
        console.log('model....', this.model)
      },
      // 获取欠费里面有滞纳金的最大的一笔欠费的角标
      getMaxIndex(val) {
        let firstOverdue = val.findIndex((item) => {return item.overdue > 0})
        if (firstOverdue === -1) {
          return 0
        } else {
          let lastOverdue = val.slice(firstOverdue).findIndex((item) => {return item.overdue === 0}) - 1
          return lastOverdue < 0 ? 0 : firstOverdue + lastOverdue
        }
      }
    }
  }
</script>
<style lang="less">
  #tooltip div {
    height: auto;
  }
</style>
